package com.sskj.uboex.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.level.bean.TradeListBean;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.uboex.R;
import com.sskj.uboex.presenter.FiveFragmentPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.APP_FRAGMENT_FIVE)
public class FiveFragment extends BaseFragment<FiveFragmentPresenter> {
    @BindView(R.id.tvFirst)
    TextView tvFirst;
    @BindView(R.id.tvSecond)
    TextView tvSecond;
    @BindView(R.id.recyclerViewBuy)
    RecyclerView recyclerViewBuy;
    @BindView(R.id.recyclerViewSell)
    RecyclerView recyclerViewSell;
    @BindView(R.id.coolRefreshViewBuy)
    CoolRefreshView coolRefreshViewBuy;
    @BindView(R.id.coolRefreshViewSell)
    CoolRefreshView coolRefreshViewSell;

    private boolean isFirst = true;
    private SlimAdapter slimAdapterBuy;
    private SlimAdapter slimAdapterSell;
    private String type = "1";
    private MVCCoolHelper<List<TradeListBean>> mvcHelperBuy;
    private MVCCoolHelper<List<TradeListBean>> mvcHelperSell;


    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_five;
    }

    @Autowired
    String code;

    @Override
    public FiveFragmentPresenter getPresenter() {
        return new FiveFragmentPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        ClickUtil.click(tvFirst, () -> {
            if (!isFirst) {
                isFirst = true;
                changeFirst();
            }
        });
        ClickUtil.click(tvSecond, () -> {
            if (isFirst) {
                isFirst = false;
                changeSecond();
            }
        });
        initRecy();
    }


    private void initRecy() {
        recyclerViewBuy.setLayoutManager(new LinearLayoutManager(getActivity()));

        slimAdapterBuy = SlimAdapter.create().register(R.layout.app_recy_item_five_buy, new SlimInjector<TradeListBean>() {
            @Override
            public void onInject(TradeListBean data, IViewInjector injector, List payloads) {

                injector
                        .text(R.id.tvRank, slimAdapterBuy.getData().indexOf(data) + "")
                        .text(R.id.tvPrice, data.getBuyPrice())
                        .text(R.id.tvNum, data.getEntrustLot());
            }
        });
        slimAdapterBuy.attachTo(recyclerViewBuy).updateData(new ArrayList());

        ModelRx2DataSource<TradeListBean> dataSourceBuy = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<TradeListBean>)
                page -> mPresenter.getData(page, 10, code, type, "1"), 10);
        mvcHelperBuy = new MVCCoolHelper<>(coolRefreshViewBuy);
        mvcHelperBuy.setDataSource(dataSourceBuy);
        mvcHelperBuy.setAdapter(slimAdapterBuy);
        mvcHelperBuy.refresh();

        recyclerViewSell.setLayoutManager(new LinearLayoutManager(getActivity()));

        slimAdapterSell = SlimAdapter.create().register(R.layout.app_recy_item_five_sell, new SlimInjector<TradeListBean>() {
            @Override
            public void onInject(TradeListBean data, IViewInjector injector, List payloads) {

                injector
                        .text(R.id.tvRank, slimAdapterSell.getData().indexOf(data) + "")
                        .text(R.id.tvPrice, data.getBuyPrice())
                        .text(R.id.tvNum, data.getEntrustLot());
            }
        });
        ModelRx2DataSource<TradeListBean> dataSourceSell = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<TradeListBean>)
                page -> mPresenter.getData(page, 10, code, type, "2"), 10);
        mvcHelperSell = new MVCCoolHelper<>(coolRefreshViewSell);
        mvcHelperSell.setDataSource(dataSourceSell);
        mvcHelperSell.setAdapter(slimAdapterSell);
        mvcHelperSell.refresh();
        slimAdapterSell.attachTo(recyclerViewSell).updateData(new ArrayList());

    }

    private void changeSecond() {
        tvFirst.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.app_575));
        tvFirst.setBackgroundColor(ContextCompat.getColor(App.INSTANCE, R.color.app_e9e));
        tvSecond.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.app_253));
        tvSecond.setBackgroundColor(ContextCompat.getColor(App.INSTANCE, R.color.commonWhite));
        type = "2";
        mvcHelperBuy.refresh();
        mvcHelperSell.refresh();
    }

    private void changeFirst() {
        tvFirst.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.app_253));
        tvFirst.setBackgroundColor(ContextCompat.getColor(App.INSTANCE, R.color.commonWhite));
        tvSecond.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.app_575));
        tvSecond.setBackgroundColor(ContextCompat.getColor(App.INSTANCE, R.color.app_e9e));
        type = "1";
        mvcHelperBuy.refresh();
        mvcHelperSell.refresh();

    }

    @Override
    public void initData() {

    }


}
